package com.jack.leetcode.每日一题;

/*** 
 * @author zhangjie
 * @date 2020/5/7 21:43
 */
public class 另一个树的子树 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static boolean isSubtree(TreeNode s, TreeNode t) {
        if (t == null) {
            return true;
        }
        if (s == null) {
            return false;
        }
        return isSubtree(s.left, t) || isSubtree(s.right, t) || isSameTree(s, t);
    }

    private static boolean isSameTree(TreeNode s, TreeNode t) {
        if (s == null && t == null) {
            return true;
        }
        if (s == null || t == null) {
            return false;
        }
        if (s.val != t.val) {
            return false;
        }
        return isSameTree(s.left, t.left) & isSameTree(s.right, t.right);
    }

    public static void main(String[] args) {
        TreeNode s = new TreeNode(3,
                new TreeNode(4, new TreeNode(1), new TreeNode(2, new TreeNode(0), null)),
                new TreeNode(5));
        StringBuilder a = new StringBuilder("0142");
        StringBuilder b = new StringBuilder("142");
        if (a.toString().contains(b.toString()) && (a.indexOf(b.toString()) == 0 || a.indexOf(b.toString()) == (a.length() - b.length()))) {
            System.out.println(true);
        } else {
            System.out.println(false);
        }
    }
}
